#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"
#include "../DEMURA/Demura.h"
void SD5302_Enable_Flash_Control_and_SPI_Flash_En(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	u16 temp;
	u16 RxBuff[8];
	u8 bufferx[] = {0x07,0xef,0x00,0x00,0x00,0x00,0x00,0xab};
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	buffer1[0] = 0x04;
	buffer1[1] = 0xf0;
	buffer1[2] = 0xa5;
	buffer1[3] = 0x0f;
	buffer1[4] = 0xf0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xfd;
	buffer1[2] = 0x00;	
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	
	bufferx[0] = 0x05;
	bufferx[5] = 0x01;
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	
	bufferx[0] = 0x07;
	bufferx[5] = 0x00;
	bufferx[7] = 0x9f;
	SSD2828_W_Array(SigMode,channel,bufferx,0);
	
	bufferx[0] = 0x05;
	bufferx[5] = 0x01;
	SSD2828_W_Array(SigMode,channel,bufferx,0);
		
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Long_Read);
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0008); //return package 
	SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
	buffer1[0] = 0x01;
	buffer1[1] = 0xf1;
	SSD2828_W_Array(SigMode,channel,buffer1,0); 
	
	delay_ms(15);
	SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
	SSD2828_W_Cmd(SigMode,channel,0xFF);
	for(i=0;i<8;i++)  //readback num
	{
		if(i==0)
				temp=SSD2828_R_Reg(SigMode,channel,0xFF);
			else
				temp=SSD2828_R_Reg_Flow(SigMode,channel);	
			RxBuff[0+i] = temp;
			RxBuff[1+i] = temp>>8;
			i++;
	}
	buffer[4] = RxBuff[3];
	buffer[5] = RxBuff[4];
	buffer[6] = RxBuff[5];

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	
}
void SD5302_Inter_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
//	u16 temp;
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	
	buffer1[0] = 0x01;
	buffer1[1] = 0x28;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(500);
	
	buffer1[0] = 0x04;
	buffer1[1] = 0xf0;
	buffer1[2] = 0xa5;
	buffer1[3] = 0x0f;
	buffer1[4] = 0xf0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xfd;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xe0;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3); 
}
                                    
void SD5302_Exit_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}
                             
void SD5302_Flash_Write_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{

		u16 temp = 0;
		u16 data;
		u8 buffer1[] = {0x04,0xf0,0xa5,0x0f,0xf0};
		u8 buffer2[] = {0x02,0xfd,0x00};
		u8 buffer3[] = {0x05,0xbd,0x71,0x44,0x00,0x00};
		u8 buffer4[] = {0x02,0xef,0x01};
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);	

		SSD2828_W_Array(SigMode,channel,buffer1,0);
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		SSD2828_W_Array(SigMode,channel,buffer3,0);   
		SSD2828_W_Array(SigMode,channel,buffer4,0);

		
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Long_Read);
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package 
		SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
		do{
				buffer1[0] = 0x01;
				buffer1[1] = 0xf1;
				SSD2828_W_Array(SigMode,channel,buffer1,0); 
				
				delay_ms(15);
				SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
				SSD2828_W_Cmd(SigMode,channel,0xFF); 
				
				data = SSD2828_R_Reg(SigMode,channel,0xFF);
				buffer[4] = data;
				buffer[5] = data>>8;
				delay_ms(5);
				temp++;
		}while(((buffer[5]&0x01) != 0x00) && temp < 2000);
		if(temp>=2000 || (buffer[4]&0x01)==0x01)
		{
			buffer[4] = 0xff;
		}		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);	
}                       




void SD5302_Flash_Check_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	  u8 bufferx[2];
		u16 temp;
		u8 buffer1[] = {0x03,0x7f,0xf0,0x20};
		u8 buffer2[] = {0x04,0xf0,0xa5,0x0f,0xf0};
		u8 buffer3[] = {0x08,0xef,0x00,0x00,0x00,0x00,0x00,0x07,0xab};
		u8 buffer4[] = {0x05,0xef,0x00,0x00,0x00,0x01};
		u8 buffer5[] = {0x0d,0xef,0x00,0x00,0x00,0x00,0x00,0x07,0x03,0x00,0x00,0x00,0x83,0x1f};
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
		delay_ms(15);
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		SSD2828_W_Array(SigMode,channel,buffer2,0);
		SSD2828_W_Array(SigMode,channel,buffer3,0);
		SSD2828_W_Array(SigMode,channel,buffer4,0);
		delay_ms(15);
		for(int i=0;i<4;i++)
		{
			if(i==0)
			{
				buffer5[11] = 0x00;
			}
			else if(i==1)
			{
				buffer5[11] = 0x01;
			}
			else if(i==2)
			{
				buffer5[11] = 0x02;
			}
			else if(i==3)
			{
				buffer5[11] = 0x03;
			}
				SSD2828_W_Array(SigMode,channel,buffer5,0);
				SSD2828_W_Array(SigMode,channel,buffer4,0);
			
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Long_Read);
			SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package 
			SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); //reset
			
				bufferx[0] = 0x01;
				bufferx[1] = 0xf1;
				SSD2828_W_Array(SigMode,channel,bufferx,0); 
				
				delay_ms(15);
				SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
				SSD2828_W_Cmd(SigMode,channel,0xFF); 
				
				SSD2828_W_Cmd(SigMode,channel,0xFA);
				temp=SPI3_Read_u16_Data(channel);
				buffer[4+i*2] = temp;
				buffer[4+i*2+1] = temp>>8;
				delay_ms(5);
			
				SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
				
				STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);	
		}
		

}
                                  
void SD5302_Flash_Erase_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{	
	u8 buffer1[] = {0x04,0xf0,0xa5,0x0f,0xf0};
	u8 buffer2[] = {0x02,0xfd,0x00};
	u8 buffer3[] = {0x07,0xef,0x00,0x00,0x00,0x00,0x00,0xab};
	u8 buffer4[] = {0x05,0xef,0x00,0x00,0x00,0x01};
	u8 buffer5[] = {0x07,0xef,0x00,0x00,0x00,0x00,0x00,0x06};
	u8 buffer6[] = {0x05,0xef,0x00,0x00,0x00,0x01};
	u8 buffer7[] = {0x07,0xef,0x00,0x00,0x00,0x00,0x00,0xc7};
	u8 buffer8[] = {0x05,0xef,0x00,0x00,0x00,0x01};
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer2,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer3,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer4,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer5,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer6,0);		
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer7,0);
		delay_ms(15);
	SSD2828_W_Array(SigMode,channel,buffer8,0);

	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //陏?Y3??crc????

}



void SD5302_Demura_Function_ON(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);	
	buffer1[0] = 0x04;
	buffer1[1] = 0xF0;
	buffer1[2] = 0xA5;
	buffer1[3] = 0x0F;
	buffer1[4] = 0xF0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xFD;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xC8;
	buffer1[2] = 0x33;
//	buffer1[3] = 0x55;
//	buffer1[4] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	
}

void SD5302_Demura_Function_OFF(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);	
	buffer1[0] = 0x04;
	buffer1[1] = 0xF0;
	buffer1[2] = 0xA5;
	buffer1[3] = 0x0F;
	buffer1[4] = 0xF0;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xFD;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	buffer1[0] = 0x02;
	buffer1[1] = 0xC8;
	buffer1[2] = 0x30;
//	buffer1[3] = 0x55;
//	buffer1[4] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}
void SD5302_GM056_Write_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buf2PC[10];
	DemuraTransOnce=120;	
	if(USB_Rx_Demura==0)//第一笔数据
	{	
			buffer[1] = 0x2c;
			USB_Rx_Demura=1; //第一次进入Demura数据写入状态
	}
	else buffer[1] = 0x3c;
	buffer[0] = 0x79;         
	for(i=2;i<USB_ReceivedCount+2;i++)
	{
		buffer[i+VCP_Receive_True_num] = USB_Rx_Buffer[i-2];  //
	}
	VCP_Receive_True_num+=USB_ReceivedCount;  //统计是否攒够一行
	DemuraRecivedNum+=USB_ReceivedCount;	  //统计总的接收数量	
	if((VCP_Receive_True_num==DemuraTransOnce)||(DemuraRecivedNum==USB_Rx_Demura_Total_Num)) //写入单笔数据,单次写入OLED.H_pixel*3字节
	{																					//最后一笔会多送数据，但是多送的数据不会被IC接收	

		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);			
		SSD2828_W_Array(SigMode,channel,buffer,0);
		VCP_Receive_True_num=0;
		
		if(DemuraRecivedNum==USB_Rx_Demura_Total_Num)//最后一笔，状态重置
		{
			DemuraRecivedNum=0; //清空 累计接受数据
			USB_Rx_Demura=0;	  //清空 Demura downloading状态
			Demura_START_FLAG=0;//清空 开始Demura传输的标志位
			STM2PC(pdev,CDC_IN_EP,buf2PC,5+3);           //返回上位机5个byte数据
		}
	}
}
void ProcessForDmuR2F( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //????
					SD5302_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //????
					SD5302_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //????
					SD5302_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //????
					SD5302_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //????
					SD5302_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					SD5302_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					SD5302_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					SD5302_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			default:
				break;												
	}
}
//********************************************************************************************
